package com.ycu.jk16.mall.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ycu.jk16.mall.domain.User;
import com.ycu.jk16.mall.service.UserService;
import com.ycu.jk16.mall.util.common.PageUtil;
import com.ycu.jk16.mall.util.common.ResponseJsonUtils;

@Controller
@RequestMapping(value = "/manage/user")
public class AdminUserController {
	@Autowired
	@Qualifier("userService")
	private UserService userService;

	/**
	 * 
	 * @Title login @Description TODO(后台管理员登陆) @param @param
	 *        username @param @param password @param @param mav @param @param
	 *        response @return void @throws
	 */
	@RequestMapping(value = "/login")
	public void login(String username, String password, HttpServletResponse response) {

		Map<String, Object> data = new HashMap<String, Object>();
		User user = userService.login(username, password);

		try {
			// 测试catch块
			user.getId();

			// 加入json数据
			data.put("status", 0);
			data.put("data", user);
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "密码错误");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	/**
	 * 
	 * @Title list  
	 * @Description TODO(获取用户列表)  
	 * @param @param pageSize
	 * @param @param pageNum
	 * @param @param response
	 * @return void
	 * @throws
	 */
	@RequestMapping(value = "/list")
	public void list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> indata = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		// 添加参数
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		List<User> userList = userService.getUserList(params);

		try {
			// catch测试块
			userList.get(0);

			PageUtil page = new PageUtil(pageNum, pageSize, userService.countUser());

			data.put("status", 0);
			indata.put("pageNum", pageNum);
			indata.put("pageSize", pageSize);
			indata.put("size", userList.size());
			indata.put("total", page.getRecordCount());
			indata.put("pages", page.getTotalPage());
			indata.put("list", userList);
			indata.put("firstPage", 1);
			indata.put("prePage", page.hasPreviousPage(pageNum) ? pageNum - 1 : pageNum);
			indata.put("nextPage", page.hasNextPage(pageNum) ? pageNum + 1 : pageNum);
			indata.put("lastPage", page.getTotalPage());
			indata.put("isFisrtPage", page.isFirstPage(pageNum));
			indata.put("isLastPage", page.isLastPage(pageNum));
			indata.put("hasPreviousPage", page.hasPreviousPage(pageNum));
			indata.put("hasNextPage", page.hasNextPage(pageNum));

			data.put("data", indata);
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "没有权限");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}
}
